<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<a href="rewards_punishments.jsp">返回</a>
<a href="index1.jsp">主页</a><br>
<table border="1" width="100%" >
    <tr>
        <th width="40">序号</th>
        <th width="40">学号</th>
        <th width="40">姓名</th>
        <th width="40">奖惩状态</th>
        <th width="40">更改状态时间</th>
    </tr>
    <%
        int flag=0;
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("stuid");
        String status = request.getParameter("rewards_punish");
        if(status.equals("null"))
            flag=1;
        String year = request.getParameter("year");
        String month = request.getParameter("month");
        String day = request.getParameter("day");
        String Date =year+"-"+month+"-"+day;
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //连接数据库
        Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3307/file?useSSL=FALSE&serverTimezone=UTC"
                , "root", "123456");
        Statement DataStatement = DataConnection.createStatement();
        ResultSet UserIsExistResult = DataStatement.executeQuery("select user_id from student" +
                " where user_id = '" + id+ "'");
        //如果存在
        if (UserIsExistResult.next()) {
            if(flag==0){
                DataStatement.executeUpdate(
                        "update dangan set Rewards_Punishment='" + status + "',Change_RP_Time='" + Date + "'where " +
                                "user_id='" + id + "'");
            }else {
                DataStatement.executeUpdate(
                        "update dangan set Rewards_Punishment='',Change_RP_Time='" + Date + "'where " +
                                "user_id='" + id + "'");
            }

            ResultSet rs = DataStatement.executeQuery("select A.user_id,user_name,Rewards_Punishment,Change_RP_Time from student A,dangan B " +
                    "where A.user_id=B.user_id and A.user_id='"+id+"'");
            int count = 1;
            while (rs.next()) {
                out.print("<tr>");
                out.print("<td>" + (count++) + "</td>");
                out.print("<td>" + rs.getString("user_id") + "</td>");
                out.print("<td>" + rs.getString("user_name") + "</td>");
                out.print("<td>" + rs.getString("Rewards_Punishment") + "</td>");
                out.print("<td>" + rs.getString("Change_RP_Time") + "</td>");
                out.print("</tr>");
            }
        }
        else {
            out.print("<tr>");
            out.print("<h1>该学生学号不存在！<h1>");
            out.print("</tr>");
        }
    %>
</table>
<%@include file="footer.jsp" %>
</body>
</html>
